//
//  LTFTopicContentView.m
//  NeteaseNews
//
//  Created by liutaifeng on 16/4/20.
//  Copyright © 2016年 liutaifeng. All rights reserved.
//

#import "LTFTopicContentView.h"
#import "LTFTopicTopView.h"
#import "LTFTopicIntrduceView.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "LTFTopicContentTableViewCell.h"
#import "LTFTopicQuestionModel.h"
#import "LTFTopicAnswerModel.h"
#import "IsNight.h"
#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height
@interface LTFTopicContentView ()
<
 LTFTopicIntrduceViewDelegate,
 UITableViewDelegate,
 UITableViewDataSource
>
@property (nonatomic, retain) LTFTopicTopView *topView;
@property (nonatomic, retain) LTFTopicIntrduceView *intrduceView;
@property (nonatomic, retain) UIView *headView;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *tableViewQuestionArray;
@property (nonatomic, retain) NSMutableArray *tableViewAnswerArray;
@property (nonatomic, retain) NSMutableArray *array;
//输入问题
@property (nonatomic, retain) UITextField *textField;
@end

@implementation LTFTopicContentView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
        [self createData];
    }
    return self;
}
- (void)setString:(NSString *)string {
    if (_string != string) {
        [_string release];
        _string = [string copy];
        _topView.string = self.string;
        _intrduceView.string = self.string;
        [self createData];
    }
}

- (void)createView {
    
    
    //tableView的头视图
    self.headView = [[UIView alloc] init];
    
    [self addSubview:_headView];
    
    //顶部图片
    self.topView = [[LTFTopicTopView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3)];
    [_headView addSubview:self.topView];
    
    
    //介绍
    self.intrduceView = [[LTFTopicIntrduceView alloc] initWithFrame:CGRectMake(0, HEIGHT / 3, WIDTH, HEIGHT / 2 - 50)];
    [_headView addSubview:_intrduceView];
    
    self.intrduceView.delegate = self;
    [self.intrduceView.seg addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];
    
    //问题与回答
    self.tableView = [[UITableView alloc] init];
    [self addSubview:_tableView];
    //协议
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //注册
    [_tableView registerClass:[LTFTopicContentTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LTFTopicContentTableViewCell class])];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tag = 1001;
    
    self.isAppear = NO;

}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y > HEIGHT / 3) {
        self.isAppear = YES;
        
    }
    else {
        self.isAppear = NO;
        
    }
    [self.delegate appear:self.isAppear];
}
- (void)click:(UISegmentedControl *)seg {
    switch (seg.selectedSegmentIndex) {
        case 0:
            self.intrduceView.isclick = NO;
            [self createData];
            break;
        case 1:
            self.intrduceView.isclick = YES;
            [self createData];
            break;
        default:
            break;
    }
}
//设置高度
- (void)sendHeight:(CGFloat)height {
        self.intrduceView.frame = CGRectMake(0, HEIGHT / 3, WIDTH, height);
    self.headView.frame = CGRectMake(0, 0, WIDTH, height + HEIGHT / 3);
    self.tableView.tableHeaderView = self.headView;
    self.tableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT - 48);
}

//tableView协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableViewQuestionArray.count;
}
//tableview协议
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //重用
    LTFTopicContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LTFTopicContentTableViewCell class]) forIndexPath:indexPath];
    //提问model
    LTFTopicQuestionModel *questionModel = self.tableViewQuestionArray[indexPath.row];
    //用户名
    cell.model.userName = questionModel.userName;
    //用户头像
    cell.model.userHeadPicUrl = questionModel.userHeadPicUrl;
    //问题
    cell.model.questionContent = questionModel.content;

    //回复model
    
    LTFTopicAnswerModel *answerModel = self.tableViewAnswerArray[indexPath.row];
    //回复头像
    cell.model.specialistHeadPicUrl = answerModel.specialistHeadPicUrl;
    //名字
    cell.model.specialistName = answerModel.specialistName;
    //回复内容
    cell.model.replyContent = answerModel.content;
    //计算时间戳
    
    cell.model.cTime = answerModel.cTime;
    //回复数
    cell.model.replyCount = answerModel.replyCount;
    //点赞数
    cell.model.supportCount = answerModel.supportCount;
    return cell;
}
//cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    //定义字典
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    //问题model
    LTFTopicQuestionModel *questionModel = self.tableViewQuestionArray[indexPath.row];
    //回复model
    LTFTopicAnswerModel *answerModel = self.tableViewAnswerArray[indexPath.row];
    //问题内容
    NSString *str = questionModel.content;
    //回复内容
    NSString *string = answerModel.content;
    //计算问题内容高度
    CGRect questionRect =[str boundingRectWithSize:CGSizeMake(WIDTH - 85, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //计算回复高度
    CGRect answerRect =[string boundingRectWithSize:CGSizeMake(WIDTH - 85, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回高度
    return answerRect.size.height + questionRect.size.height + 200;
}
//解析地址
- (void)createData {
    self.array = [NSMutableArray array];
    self.tableViewQuestionArray = [NSMutableArray array];
    self.tableViewAnswerArray = [NSMutableArray array];
    NSString *string = [NSString stringWithFormat:@"http://c.3g.163.com/newstopic/qa/%@.html", self.string];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:string parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dictionary = responseObject;
        NSDictionary *data = dictionary[@"data"];
        //NSLog(@"%d", self.intrduceView.isclick);
        if (self.intrduceView.isclick == NO) {
            self.array = data[@"latestList"];
            
        }
        else {
            self.array = data[@"hotList"];
        }
        for (NSDictionary *dic in self.array) {
            NSDictionary *question = dic[@"question"];
            //疑问model
            LTFTopicQuestionModel *questionModel = [[LTFTopicQuestionModel alloc] init];
            [questionModel setValuesForKeysWithDictionary:question];
            [self.tableViewQuestionArray addObject:questionModel];
            [questionModel release];
            //回复Model
            NSDictionary *answer = dic[@"answer"];
            LTFTopicAnswerModel *answerModel = [[LTFTopicAnswerModel alloc] init];
            [answerModel setValuesForKeysWithDictionary:answer];
            [self.tableViewAnswerArray addObject:answerModel];
            [answerModel release];
        }
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
